package org.seckill.web;

import org.seckill.dto.Exposer;
import org.seckill.dto.SeckillExcution;
import org.seckill.dto.SeckillResult;
import org.seckill.entity.Seckill;
import org.seckill.enums.SeckillStateEnum;
import org.seckill.exception.RepeatkillException;
import org.seckill.exception.SeckillCloseException;
import org.seckill.service.SecKillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * Created by sanmao on 2017/6/16.
 */
@Controller
@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    private SecKillService secKillService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        System.out.println("list--------------------");
        List<Seckill> secKillList = secKillService.getSecKillList();
        model.addAttribute("list", secKillList);

        return "list";
    }

    @RequestMapping(value = "/{seckillId}/detail", method = RequestMethod.GET)
    private String detail(Long seckillId, Model model) {

        Seckill seckill = secKillService.getSecKillById(seckillId);

        model.addAttribute("seckill", seckill);
        return "detail";

    }

    @RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SeckillResult<Exposer> exposer(Long seckillId) {
        SeckillResult<Exposer> result = null;
        try {

            Exposer exposer = secKillService.exportSeckillUrl(seckillId);
            result = new SeckillResult<Exposer>(true, exposer);

        } catch (Exception e) {
            result = new SeckillResult<Exposer>(false, e.getMessage());
        }


        return result;
    }

    @RequestMapping(value = "/{seckillId}/{md5}/execution",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SeckillResult<SeckillExcution> excte(Long seckillId, String md5, @CookieValue(value = "killphone", required = false) Long phone) {

        if (phone == null) {
            return new SeckillResult<SeckillExcution>(false, "用户未注册");
        }

        SeckillResult<SeckillExcution> result;
        try {
            SeckillExcution excution = secKillService.executeSeckill(seckillId, phone, md5);
            return new SeckillResult<SeckillExcution>(true,excution);

        }catch (RepeatkillException re){
            System.out.println(re.getMessage());
            SeckillExcution excution=new SeckillExcution(seckillId,SeckillStateEnum.REPEAT_KILL);
            return new SeckillResult<SeckillExcution>(false,excution);
        }catch (SeckillCloseException se){
            SeckillExcution excution=new SeckillExcution(seckillId,SeckillStateEnum.END);
            return new SeckillResult<SeckillExcution>(false,excution);
        }

        catch (Exception e){
            SeckillExcution excution=new SeckillExcution(seckillId,SeckillStateEnum.INNERR_ERROR);
            return new SeckillResult<SeckillExcution>(false,excution);
        }

    }

    @RequestMapping(value = "/tiome/now",method = RequestMethod.GET)
    public SeckillResult<Long> time(){
        Date now=new Date();
        return new SeckillResult(true,now.getTime());
    }

}
